﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using System.Linq;
using System.Data.Linq;
using Manager.FormAdd_Edit;
using Manager.Common;

namespace Manager.FormMain
{
    public partial class Frm_BarButton_TienTe : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();

        public Frm_BarButton_TienTe()
        {
            InitializeComponent();
        }

        private void butThemMoi_Click(object sender, EventArgs e)
        {
            frm_Currency frm = new frm_Currency();
            frm.ShowDialog();
            Frm_BarButton_TienTe_Load(null, null);
        }

        private void butHieuChinh_Click(object sender, EventArgs e)
        {
            try
            {
                int index = gridView1.FocusedRowHandle;
                var madv = gridView1.GetRowCellValue(index, MaDonVi).ToString();
                var tienTe = db.TienTes.Single(i => i.MaDonVi == madv);
                frm_Currency frm = new frm_Currency(tienTe);
                frm.ShowDialog();
                Frm_BarButton_TienTe_Load(null, null);
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_TienTe:butHieuChinh_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dr = MessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    int index = gridView1.FocusedRowHandle;
                    var madv = gridView1.GetRowCellValue(index, MaDonVi).ToString();
                    var tienTe = db.TienTes.Single(i => i.MaDonVi == madv);
                    db.TienTes.DeleteOnSubmit(tienTe);
                    db.SubmitChanges();
                    MessageBox.Show("Xóa thành công.");
                    Frm_BarButton_TienTe_Load(null, null);
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("Frm_BarButton_TienTe:butXoa_Click" + ex.Message + "===>" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void Frm_BarButton_TienTe_Load(object sender, EventArgs e)
        {
            db.Refresh(RefreshMode.OverwriteCurrentValues, db.TienTes);
            gridControl1.DataSource = db.TienTes.ToList();
        }
    }
}